package com.hyxt.DTO;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hyxt.api.T809ClientService;
import com.hyxt.api.T809ServerService;
import com.hyxt.api.TermianalLinkServer;
import com.hyxt.dao.RedisServiceDAO;
import com.hyxt.utils.IRedisMessageListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author shenp
 * @version v1.0
 * @Description 监听t809链路管理消息
 * @Date: Create in 15:47 2017/11/28
 * @Modifide By:
 **/
@Component
public class T809ManagerListener extends IRedisMessageListener {
    @Reference(version = "1.0.0")
    T809ClientService t809ClientService;
    @Reference(version = "1.0.0")
    T809ServerService t809ServerService;
    @Reference(version = "1.0.0")
    RedisServiceDAO redisServiceDAO;
    @Reference(version = "1.0.0")
    TermianalLinkServer termianalLinkServer;

    @Override
    public void onMessage(String channel, String message) {
        System.out.println(channel);
        System.out.println(message);
        JSONObject jsonObject = JSON.parseObject(message, JSONObject.class);
        //唯一标识
        Integer sign = (Integer) jsonObject.get("sign");
        //主从标识
        Integer mainOrBranch = (Integer ) jsonObject.get("mainOrBranch");
        //获取接收转发标识
        Integer accessOrForward = (Integer) jsonObject.get("accessOrForward");
        //处理类型
        Integer type = (Integer) jsonObject.get("type");
        //获取ip
        String ip = (String)jsonObject.get("ip");
        //获取端口
        Integer port = (Integer) jsonObject.get("port");
        switch (type){
            //启动客户端
            case 0:
                t809ClientService.startClient(ip,port,mainOrBranch,sign,accessOrForward);
                break;
            //启动服务端
            case 1:
                t809ServerService.startServer(port,mainOrBranch,accessOrForward);
                break;
            //销毁链路
            case 2:
                //根据唯一标识和主从标识判断链路在客户端还是服务端管理
                String position =(String) redisServiceDAO.getMap(sign+mainOrBranch+"",0).get("position");
                if ("client".equals(position)){
                    t809ClientService.deleteChannel(sign,mainOrBranch);
                }else{
                    t809ServerService.deleteChannel(sign,mainOrBranch);
                }
                break;
            default:
        }
    }
}
